package com.kizitonwose.calendarviewsample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.flipboard.bottomsheet.commons.MenuSheetView
import sample.note.R
import sample.note.SearchListFragment
import sample.note.EventListFragment
import sample.note.databinding.HomeActivityBinding


class HomeActivity : AppCompatActivity() {

    private lateinit var menuSheetView: MenuSheetView
    internal lateinit var binding: HomeActivityBinding
    private var fragments: HashMap<Int, Fragment> = HashMap<Int, Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = HomeActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        //setSupportActionBar(binding.homeToolbar)

        load(R.id.navigation_shop)
        // Navigation
        navi()

        // plugin
        plugin()
    }
    private fun plugin(){
        binding.sheet.peekOnDismiss = true
        menuSheetView = MenuSheetView(this, MenuSheetView.MenuType.GRID, "Plugin", MenuSheetView.OnMenuItemClickListener { _ ->
            if (binding.sheet.isSheetShowing()) {
                binding.sheet.dismissSheet()
            }
            true
        })
        menuSheetView.inflateMenu(R.menu.list_menu)
    }

    private fun load(itemId: Int) {
        if (!fragments.containsKey(itemId)) {
            when (itemId){
                R.id.navigation_gifts -> {
                    fragments.put(itemId, SearchListFragment())
                }
                R.id.navigation_shop -> {
                    fragments.put(itemId, EventListFragment())
                }
                else -> {
                    fragments.put(itemId, Example3Fragment())
                }
            }
        }
        val fragment = fragments[itemId]!!
        supportFragmentManager.popBackStack();
        if (fragment.isAdded()) {
            supportFragmentManager.beginTransaction().show(fragment)
                    .addToBackStack(fragment.javaClass.simpleName)
                    .commit()

            return
        }
        supportFragmentManager.beginTransaction()
                .run {
                    return@run setCustomAnimations(
                            R.anim.slide_in_right,
                            R.anim.slide_out_left,
                            R.anim.slide_in_left,
                            R.anim.slide_out_right
                    )
                }
                .add(R.id.homeContainer, fragment, fragment.javaClass.simpleName)
                .addToBackStack(fragment.javaClass.simpleName)
                .commit()
    }
    fun navi() {
        binding.navigation.setOnNavigationItemSelectedListener { item ->
            if (item.itemId == R.id.navigation_profile) {
                binding.sheet.showWithSheetView(menuSheetView)
                return@setOnNavigationItemSelectedListener true
            }
            load(item.itemId)
            true
        }
    }
}
